# define a set of string with may-be useful readable name
# this file is meant to be included in a CMakeLists.txt
# not as a standalone CMake script
set(CMAKE_INFO_COMPILER_NAME "")
set(CMAKE_INFO_COMPILER_SHOWNAME "")
set(CMAKE_INFO_PLATFORM_NAME "")
set(CMAKE_INFO_PLATFORM_BITNESS "")

if(WIN32)
  set(CMAKE_INFO_OS_SHORT_NAME "nt")
elseif(UNIX)
  if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(CMAKE_INFO_OS_SHORT_NAME "linux")
  else(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    MESSAGE(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM}")
  endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif(WIN32)

# Compilers
# taken from http://predef.sourceforge.net/precomp.html#sec34
if(MSVC)
  if(MSVC_VERSION LESS 1600)
    MESSAGE(FATAL_ERROR "Cannot support MSVC compiler earlier than VS2010.")
  endif(MSVC_VERSION LESS 1600)
  if(MSVC_VERSION EQUAL 1600)
    set(CMAKE_INFO_COMPILER_NAME "vc-10.0") #Visual Studio 2010
    set(CMAKE_INFO_COMPILER_SHOWNAME "msvc10")
  endif(MSVC_VERSION EQUAL 1600)
  if(MSVC_VERSION EQUAL 1700)
    set(CMAKE_INFO_COMPILER_NAME "vc-11.0") #Visual Studio 2012
    set(CMAKE_INFO_COMPILER_SHOWNAME "msvc11")
  endif(MSVC_VERSION EQUAL 1700)
  if(MSVC_VERSION EQUAL 1800)
    set(CMAKE_INFO_COMPILER_NAME "vc-12.0") #Visual Studio 2013
    set(CMAKE_INFO_COMPILER_SHOWNAME "msvc12")
  endif(MSVC_VERSION EQUAL 1800)
 
  if(CMAKE_GENERATOR MATCHES "Win64")
    set(CMAKE_INFO_PLATFORM_NAME "ntx64")
    set(CMAKE_INFO_VS_PLATFORM_NAME "x64")
    set(CMAKE_INFO_PLATFORM_BITNESS 64)
  else(CMAKE_GENERATOR MATCHES "Win64")
    set(CMAKE_INFO_PLATFORM_NAME "ntx86")
    set(CMAKE_INFO_VS_PLATFORM_NAME "win32")
    set(CMAKE_INFO_PLATFORM_BITNESS 32)
  endif(CMAKE_GENERATOR MATCHES "Win64")
endif(MSVC)

if(UNIX OR MINGW)
  if(NOT CMAKE_INFO_ARCH_NAME)
    MESSAGE(FATAL_ERROR "If you use MinGW or GCC compiler, please specify CMAKE_INFO_GCC_PLATFORM_NAME." )
  endif(NOT CMAKE_INFO_ARCH_NAME)
  
  exec_program( ${CMAKE_C_COMPILER} ARGS -dumpversion OUTPUT_VARIABLE GCC_VERSION )
  MESSAGE(STATUS "GCC version: ${GCC_VERSION}")
  string( REPLACE "." "" GCC_VERSION_STR_FULL ${GCC_VERSION} )
  string( REGEX MATCH "[0-9]+\\.[0-9]+" GCC_VERSION_MAJOR_MINOR ${GCC_VERSION} )
  string( REPLACE "." "" GCC_VERSION_STR_MAJOR_MINOR ${GCC_VERSION_MAJOR_MINOR} )

  if(CMAKE_INFO_ARCH_NAME MATCHES "x64")
	set(CMAKE_INFO_PLATFORM_NAME "${CMAKE_INFO_OS_SHORT_NAME}x64")
    set(CMAKE_INFO_PLATFORM_BITNESS 64)
  elseif(CMAKE_INFO_ARCH_NAME MATCHES "x86")
	set(CMAKE_INFO_PLATFORM_NAME "${CMAKE_INFO_OS_SHORT_NAME}x86")
    set(CMAKE_INFO_PLATFORM_BITNESS 32)
  else(CMAKE_INFO_ARCH_NAME MATCHES "x86")
    MESSAGE(FATAL_ERROR "Value of CMAKE_INFO_GCC_PLATFORM_NAME (${CMAKE_INFO_GCC_PLATFORM_NAME}) is invalid." )
  endif(CMAKE_INFO_ARCH_NAME MATCHES "x64")
  
  if(MINGW)
    exec_program( ${CMAKE_C_COMPILER} ARGS -dumpmachine OUTPUT_VARIABLE GCC_MACHINE )
	if(GCC_MACHINE MATCHES "x86_64")
	  set(CMAKE_INFO_COMPILER_SHOWNAME "mingw64${GCC_VERSION_STR_FULL}")
	else(GCC_MACHINE MATCHES "x86_64")
      set(CMAKE_INFO_COMPILER_SHOWNAME "mingw${GCC_VERSION_STR_FULL}")
	endif(GCC_MACHINE MATCHES "x86_64")
    set(CMAKE_INFO_COMPILER_NAME "mgw-${GCC_VERSION}")
  else(MINGW)
    set(CMAKE_INFO_COMPILER_NAME "gcc-${GCC_VERSION}")
	set(CMAKE_INFO_COMPILER_SHOWNAME "gcc${GCC_VERSION_STR_FULL}")
  endif(MINGW)
  
endif(UNIX OR MINGW)
